//
//  CalculatorViewController.m
//  Calculator
//
//  Created by Hilary Muludiang on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorViewController.h"

@implementation CalculatorViewController

- (CalculatorBrain *)brain
{
	if (!brain)brain=[[CalculatorBrain alloc] init];
	return brain;
	

	
}

- (IBAction)digitPressed:(UIButton *)sender
{
	NSString *digit=[[sender titleLabel]text];
	if (userIsInTheMiddleOfTypingANumber)
	{
		[display setText:[[display text] stringByAppendingString:digit]];
	}
	else
	{
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
- (IBAction)decimalPressed:(UIButton *)sender
{
	NSString *digit=[[sender titleLabel]text];
	if (!userEnteredADecimal)
	{
	
		[display setText:[[display text] stringByAppendingString:digit]];
		userEnteredADecimal = YES;
	}
	if (!userIsInTheMiddleOfTypingANumber)
	{
		userIsInTheMiddleOfTypingANumber = YES;
	}
}

- (IBAction)changeSign:(UIButton *)sender
{
	double result = [[self brain] setSign:[[display text]doubleValue]];
	[display setText:[NSString stringWithFormat:@"%g",result]];
}

- (IBAction)operationPressed:(UIButton *)sender
{
	if (userIsInTheMiddleOfTypingANumber)
	{
		[[self brain] setOperand:[[display text]doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText:[NSString stringWithFormat:@"%g",result]];
}

- (IBAction)clearCalculator:(UIButton *)sender
{
	[[self brain] resetCalculator];
	[display setText:@"0"];
	userEnteredADecimal = NO;
}
@end
